package com.mm.update.util;

import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEByteEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 数据库配置文件加密工具类
 * chenxx
 */
@Slf4j
public class JasyptUtils {

    /**
     * Jasypt生成加密结果
     *
     * @param key   配置文件中设定的加密密码 jasypt.encryptor.password
     * @param value 待加密值
     * @return
     */
    public static String encryptPwd(String key, String value) {
        PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor();
        encryptOr.setConfig(cryptOr(key));
        String result = encryptOr.encrypt(value);
        return result;
    }


    /**
     * 解密
     *
     * @param key   配置文件中设定的加密密码 jasypt.encryptor.password
     * @param value 待解密密文
     * @return
     */
    public static String decyptPwd(String key, String value) {
        PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor();
        encryptOr.setConfig(cryptOr(key));
        String result = encryptOr.decrypt(value);
        return result;
    }

    public static SimpleStringPBEConfig cryptOr(String password) {
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(password);
        config.setAlgorithm(StandardPBEByteEncryptor.DEFAULT_ALGORITHM);
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        return config;
    }

    public static void main(String[] args) {
        //服务器帐号
        String userName = "root";
        //服务器密码
//        String passwd = "user*2018*100";
        String passwd = "Vos*2023";
        //请勿修改
        String key = "VOS";
        long time = System.currentTimeMillis();
        userName = userName + "__" + time;
        passwd = passwd + "__" + time;
        String userNameStr = encryptPwd(key, userName);
        String passwdStr = encryptPwd(key, passwd);
        SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(d.format(new Date()));
        System.out.println("密文帐号：" + userNameStr);
        System.out.println("密文密码：" + passwdStr);
    }

    /*public static void main(String[] args) throws Exception {
        System.out.println(getMessage("9CnOxQnXHyp5mgQiqDHyrN2hbFU0zXZXHD4Nz9ZRPvs="));
        System.out.println(getMessage("SZWnYWYNet6krWjhgz4m1fORuN3nqEcjG+nCdGYYMQ0="));
    }*/

    public static String getMessage(String passwd) throws Exception {
        try {

            String s = decyptPwd("VOS", passwd);
            String[] split = s.split("__");
            String message = split[0];
            String time = split[1];
            long curr = System.currentTimeMillis();
            Long passwdTime = Long.valueOf(time);
            //超过1小时密码过期
            if (curr - passwdTime > 60 * 1000 * 60) {
                throw new Exception("密码已失效");
            }
            return message;
        } catch (Exception e) {
            log.error("解密失败,密文{}",passwd,e);
           throw new Exception("帐号密码错误");
        }
    }

}